<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use think\Db;
use app\common\model\Project as Project_Model;
use app\web\validate\Project as Project_Validate;

/**
 * 库存管理--商品分类控制器
 * @package app\index\controller
 */
class Project extends Auth
{
    public $noNeedLogin = '';

    /**
     * 设置项目--修改或者添加项目
     * project_id
     *name   sort  price   add_block_price  add_block_time
     * normal_rate  dot_block_rate  add_block_rate  call_block_rate
     * overtime_queue_rate  overtime_dot_rate  overtime_add_block_rate
     * receiver_queue_rate  receiver_dot_rate  receiver_add_rate
     * recommend_rate time down_block_time block_number is_free_order is_store_recommend
     *is_user_show description
     */
    public function edit(){

        if ($this->request->isPost())
        {
            $data = $this->request->post();
            $validate = new Project_Validate();
            if (!$validate->check($data)){
                $this->result($validate->getError(),'',204);
            }
            if (key_exists('project_id',$data)&&!empty($data['project_id']))
            {
                 if(!isset($data['is_free_order'])){
                     $data['is_free_order'] = '0';
                 }
                if(!isset($data['is_store_recommend'])){
                    $data['is_store_recommend'] = '0';
                }
                if(!isset($data['is_user_show'])){
                    $data['is_user_show'] = '0';
                }
                 $update = Project_Model::where('id',$data['project_id'])->update($data);
                 if($update !== false){
                     $this->result('操作成功','',200);
                 }else{
                     $this->result('更新成功','',204);
                 }
            }else{
//                if (!empty(Project_Model::getProjectBySort($this->userInfo['store_id'],$data['sort']))){
//                    $this->result('排序号已经存在','',204);
//                }
                $find = Db::name('qt_project')->where(['store_id'=>$this->userInfo['store_id'],'name'=>$data['name']])->find();
                if(!empty($find)){
                    $this->result('项目已经存在','',204);
                }
                $data['store_id']=$this->userInfo['store_id'];
                $data['status'] = 1;
                Project_Model::create($data);
                $this->result('操作成功','',200);
            }
        }else{
            $this->result('请求错误','',204);
        }
    }

    /**
     * 设置项目--项目列表
     * token
     */
    public function lists()
    {
        if ($this->request->isPost())
        {
            $limit = $this->request->post('limit');
            if(empty($limit)){
                $limit = 10;
            }
            $list = Project_Model::where(['p.store_id'=>$this->userInfo['store_id']])
                ->alias('p')
                ->leftJoin('qt_project_category c','p.project_category_id = c.id')
                ->where(['p.status'=>'1'])
                ->field('p.*,c.name as cate_name')
                ->order('p.sort asc')
                ->paginate($limit);
            if (!empty($list))
            {
                foreach ($list as $item)
                {
                    if (isset($item['images']))
                    {
                        if (strpos($item['images'],','))
                        {
                            $arr = [];
                            foreach (explode(',',$item['images']) as $i)
                            {
                                array_push($arr,get_img_path($i));
                            }
                            $item['images_path'] = $arr;
                        }else{
                            $item['images_path'] = [get_img_path($item['images'])];
                        }
                    }
                }
            }else{
                $list = [];
            }

            $this->result('获取成功',$list,200);
        }else{
            $this->result('请求错误','',204);
        }
    }

    /**
     * 设置项目--删除项目
     * token
     */
    public function remove()
    {
        if ($this->request->isPost())
        {
            $data = $this->request->post();

            if (empty($data['project_id']))
                $this->result('缺少必要参数','',204);
            $status = Project_Model::checkDel($data['project_id']);
            if($status['code'] == 204){
                $this->result($status['msg'],'',204);
            }
            $result = Project_Model::destroy($data['project_id']);

            $result?$this->result('删除成功','',200):$this->result('删除失败','',204);
        }else{
            $this->result('请求错误','',204);
        }
    }


    /**
     * 获取项目图片
     */
    public function getImages(){
        if ($this->request->isPost())
        {
            $data = $this->request->post();

            if (empty($data['project_id']))
                $this->result('缺少必要参数','',204);

            $result = Project_Model::getImages($data['project_id']);

            $result?$this->result('查询成功',$result,200):$this->result('系统繁忙，请稍后再试','',204);
        }else{
            $this->result('请求错误','',204);
        }
    }



    /**
     * 更新项目图片
     */
    public function saveImages(){
        if ($this->request->isPost())
        {
            $data = $this->request->post();

            if (empty($data['project_id']) || empty($data['images']))
                $this->result('缺少必要参数','',204);

            $result = Db::name('qt_project')->where(['id'=>$data['project_id']])->update(['images'=>$data['images']]);
            $result?$this->result('更新成功','',200):$this->result('系统繁忙，请稍后再试','',204);
        }else{
            $this->result('请求错误','',204);
        }
    }

}
